<?php

/*
	PIE Content Management System
	John Girata
	Licensed under the GNU General Public License version 3
*/

defined('PIESECVAR') or die('Restricted Access');

class ComponentHandler
{
	function __construct()
	{
		global $db, $params;
		
		// Components are fired based on the GET data
		if(empty($_GET['action']))
			$action = $params->def_com;
		else
			$action = $db->escape($_GET['action']);
		
		$table = (ADMIN) ? '#__a_components' : '#__components';
		$db->query("SELECT * FROM `$table` WHERE `url`='$action' ORDER BY `position` ASC");
		
		if($db->get_error())
			die('A MySQL error has occurred: ' . $db->get_error());
		else if($db->get_num_rows() < 1)
			$this->fire_com('404');
		
		$com_array = $db->get_array();
		
		foreach($com_array as $item)
			$this->fire_com($item['id']);
	}
	
	function fire_com($id)
	{
		$file = COM_DIR . $id . '/' . $id . '.php';
		
		if(!file_exists($file))
			die('Component file does not exist.'); //include(COM_DIR . 'com.error.php');
		
		include($file);
	}
}

?>